package com.neuedu.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.neuedu.model.UserMessage;
import com.neuedu.model.Users;
import com.neuedu.service.UserMessageService;
import com.neuedu.service.UsersService;

@Controller
@RequestMapping( "/usermessage")
public class UserMessageAction {
	
	@Autowired
	private UserMessageService usermessageservice;
	@RequestMapping("/add")
	public void addUserMessage(UserMessage userMessage,HttpServletRequest request,HttpServletResponse response) throws Exception{
		//调用业务逻辑层进行处理 返回数据库表数据更新的行数 更新成功数量大于等于1 更新失败返回0
		int res=usermessageservice.addUserMessage(userMessage);
		System.out.println(res);
		Map<String, Object> map=new HashMap<String, Object>();
		if(res>0){
			map.put("success", true);
		}else{
			map.put("success", false);
			map.put("errorMsg", "Save user fail !!!");
		}
		String str=JSONObject.toJSONString(map);
		response.getWriter().write(str);
		
	}
	@RequestMapping(value="/update")
	public void updateUserMessage(UserMessage userMessage,HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		int res=usermessageservice.updateUserMessage(userMessage);
		Map<String, Object> map=new HashMap<String, Object>();
		if(res>0){
			map.put("success", true);
		}else{
			map.put("success", false);
			map.put("errorMsg", "update user fail !!!");
		}
		String str=JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	
	
	@RequestMapping(value="delete")
	public void deleteUsers(Integer userNo,HttpServletRequest request,HttpServletResponse response) throws Exception{
		
	
		int res=usermessageservice.deleteUserMessage(userNo);
		Map<String, Object> map=new HashMap<String, Object>();
		if(res>0){
			map.put("success", true);
		}else{
			map.put("success", false);
			map.put("errorMsg", "update user fail !!!");
		}
		String str=JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	@RequestMapping(value = "/findAll")
	public void findAll(HttpServletRequest request,HttpServletResponse response)throws Exception{
		//从客户端接收easyUI发送过来的分页控制参数
		//page是指查询第几页，rows是指每页查询多少条———这两个参数都有easy ui进行计算并发送到服务器
		int page=Integer.parseInt(request.getParameter("page"));
		int rows=Integer.parseInt(request.getParameter("rows"));
		Map<String, Object> map=new HashMap<String, Object>();
		Map<String, Object> pageMap=new HashMap<String, Object>();
		pageMap.put("startPage", (page-1)*rows);//从第几条数据开始查询
		pageMap.put("endPage", rows);//每页查询多少记录
		
		//进行分页查询
		List<UserMessage> u=usermessageservice.findAll(pageMap);
		//进行查询记录总数，把记录总数发给easy UI 用于分页控制与计算
		int total=usermessageservice.countUserMessage();
		//前后端分离的程序结构，大部分会使用json数据结构进行数据传递（也有使用xml）
		map.put("rows", u);
		map.put("total", total);
		//使用阿里巴巴的fastjon把java对象转换成Json字符串。
		String str=JSONObject.toJSONString(map);
		//使用serlvet技术把Json字符串从服务器发送到浏览器客户端（easyUI） 由easyUI进行数据显示
		response.getWriter().write(str);
	}
	   @RequestMapping("/login")
		public void login(UserMessage userMessage,
				    HttpServletRequest request,HttpServletResponse response) throws Exception{
			String userName=request.getParameter("userName");
			String password=request.getParameter("password");
			System.out.println("姓名："+userName);
			System.out.println("密码："+password);
			
			//调用业务逻辑层进行处理，进行用户的登录
			UserMessage ret = usermessageservice.findUserMessageByName(userMessage);
			
			if(ret!=null){
				//登录成功 - 记录用户的session会话中，并跳转Dao管理首页
				
				request.getSession().setAttribute("LOGIN_USER", ret);
				
				response.sendRedirect(request.getContextPath()+"/index.jsp");
				
			}else{
				//登录失败,返回登录页面
				response.sendRedirect(request.getContextPath()+"/login.jsp");
			}
		}
	   
	   @RequestMapping("/logout")
		public void logout(HttpServletRequest request,HttpServletResponse response) throws Exception{
			//1.使用会话失效
			request.getSession().invalidate();
			
			//2.使会话失效后，返回登录页面
			response.sendRedirect(request.getContextPath()+"/login.jsp");
			
		}

}
